<?php
namespace libraries;

/**
 * 数组型关系数据库
 * 数据数组就是用数组模拟关系型数据库，一条一条的，然后通过条件数组去筛选。类似SQL里的where
 */

class ArrayData{

    /**
     * 根据数组的某一字段进行排序
     * @param $array
     * @param $column
     * @param string $sort
     * @return array
     */
	public static function sort_column($array, $column, $sort='ASC'){
	    $tempArr = [];
	    foreach ($array as $key => $item){
            $tempArr[$item[$column].$key] = $item;
        }
        (strtoupper($sort) == 'ASC') ?  ksort($tempArr) : krsort($tempArr);
        return array_values($tempArr);
    }

    /**
     * 指定字段最大值（自然数）
     * @param $array
     * @param $column
     * @return int
     */
    public static function max($array, $column){
        $max = 0;
        foreach ($array as $key => $item){
            if(isset($item[$column]) && floatval($item[$column]) > $max){
                $max = floatval($item[$column]);
            }
        }
        return $max;
    }

    /**
     * 指定字段最小值（自然数）
     * @param $array
     * @param $column
     * @return int
     */
    public static function min($array, $column){
        $min = isset($array[0][$column]) ? floatval($array[0][$column]) : 0;
        foreach ($array as $key => $item){
            if(isset($item[$column]) && floatval($item[$column]) < $min){
                $min = floatval($item[$column]);
            }
        }
        return $min;
    }

    /**
     * 只查找一条，如果有多条只返回第一条
     * @param $data
     * @param $conditions
     * @return bool
     */
    public static function findOne($data, $conditions){
        foreach ($data as $line){
            $accord = true;
            foreach ($conditions as $key => $condition){
                if(!isset($line[$key]) || $line[$key] != $condition){
                    $accord = false;
                    break;
                }
            }
            if($accord){
                return $line;
            }
        }
        return null;
    }

    /**
     * 是否存在
     * @param $data
     * @param $conditions
     * @return bool
     */
    public static function exists($data, $conditions){
        foreach ($data as $line){
            $accord = true;
            foreach ($conditions as $key => $condition){
                if(!isset($line[$key]) || $line[$key] != $condition){
                    $accord = false;
                    break;
                }
            }
            if($accord){
                return true;
            }
        }
        return false;
    }

    /**
     * 根据条件查找，回调
     * @param $data
     * @param $conditions
     * @param $cb
     * @return int
     */
    public static function findCallback(&$data, $conditions, $cb){
        $count = 0;
        foreach ($data as &$line){
            $accord = true;
            foreach ($conditions as $key => $condition){
                if(!isset($line[$key]) || $line[$key] != $condition){
                    $accord = false;
                    break;
                }
            }
            if($accord){
                $count++;
                $cb($line);
            }
        }
        return $count;
    }

    /**
     * 根据条件查找，返回新数组
     * @param $data
     * @param $conditions
     * @return array
     */
    public static function find($data, $conditions){
        if(empty($data))return [];
        if(!is_array($data))return [];
        $findData = [];
        foreach ($data as $line){
            $accord = true;
            foreach ($conditions as $key => $condition){
                if(!isset($line[$key]) || $line[$key] != $condition){
                    $accord = false;
                    break;
                }
            }
            if($accord){
                $findData[] = $line;
            }
        }
        return $findData;
    }

    /**
     * 得到满足条件数据数量
     * @param $data
     * @param $conditions
     * @return array
     */
    public static function count($data, $conditions){
        if(empty($data))return [];
        if(!is_array($data))return [];
        $count = 0;
        foreach ($data as $line){
            $accord = true;
            foreach ($conditions as $key => $condition){
                if(!isset($line[$key]) || $line[$key] != $condition){
                    $accord = false;
                    break;
                }
            }
            if($accord){
                $count++;
            }
        }
        return $count;
    }

    /**
     * 移除满足条件的指定数据
     * @param $data
     * @param $conditions
     */
    public static function remove(&$data, $conditions){
        foreach ($data as $i => $line){
            $accord = true;
            foreach ($conditions as $key => $condition){
                if(!isset($line[$key]) || $line[$key] != $condition){
                    $accord = false;
                    break;
                }
            }
            if($accord){
                unset($data[$i]);
            }
        }
        //重新排键名，让其从0开始并连续
        $data = array_values($data);
    }

}

?>